package com.graphics.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.bin.activities.R;

public class CustomImageButton extends View {

	private final static int WIDTH_PADDING = 8;
	private final static int HEIGHT_PADDING = 10;
	private String label;
	private int imageResId;
	private Bitmap image;
	private boolean state;
    private static RightSideView view=null;
    
	private void initButton(Context context) {
		this.imageResId = R.drawable.redbutton;
		this.image = BitmapFactory.decodeResource(context.getResources(),
				imageResId);

		state = true;
		setFocusable(true);
		setClickable(true);
		this.setOnTouchListener(new CustomListener());
	}


	public CustomImageButton(Context context) {
		super(context);
		initButton(context);

	}

	public CustomImageButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		initButton(context);

	}

	public CustomImageButton(Context context, AttributeSet attrs,
			int defaultStyle) {
		super(context, attrs, defaultStyle);
		initButton(context);

	}

	protected void onFocusChanged(boolean gainFocus, int direction,
			Rect previouslyFocusedRect) {
		if (gainFocus == true) {
			this.setBackgroundColor(Color.rgb(255, 165, 0));
		} else {
			this.setBackgroundColor(Color.WHITE);
		}
	}

	protected void onDraw(Canvas canvas) {
		this.image = BitmapFactory.decodeResource(this.getResources(),
				imageResId);
		Paint textPaint = new Paint();
		textPaint.setColor(Color.BLACK);
		canvas.drawBitmap(image, WIDTH_PADDING / 2, HEIGHT_PADDING / 2, null);
		// canvas.drawText(label, WIDTH_PADDING / 2, (HEIGHT_PADDING / 2)+
		// image.getHeight() + 8, textPaint);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(measureWidth(widthMeasureSpec),
				measureHeight(heightMeasureSpec));
	}

	private int measureWidth(int measureSpec) {
		int preferred = image.getWidth() * 2;
		return getMeasurement(measureSpec, preferred);
	}

	private int measureHeight(int measureSpec) {
		int preferred = image.getHeight() * 2;
		return getMeasurement(measureSpec, preferred);
	}

	private int getMeasurement(int measureSpec, int preferred) {
		int specSize = MeasureSpec.getSize(measureSpec);
		int measurement = 0;

		switch (MeasureSpec.getMode(measureSpec)) {
		case MeasureSpec.EXACTLY:
			// This means the width of this view has been given.
			measurement = specSize;
			break;
		case MeasureSpec.AT_MOST:
			// Take the minimum of the preferred size and what
			// we were told to be.
			measurement = Math.min(preferred, specSize);
			break;
		default:
			measurement = preferred;
			break;
		}

		return measurement;
	}

	/**
	 * Returns the button's label.
	 */
	public String getLabel() {
		return label;
	}

	/**
	 * Returns the image's resource id.
	 */
	public int getImageResId() {
		return imageResId;
	}
    
	private class CustomListener implements OnTouchListener{

		public boolean onTouch(View v, MotionEvent event) {
			int action = event.getAction();
			switch (action) {
			case MotionEvent.ACTION_DOWN:
				if (state) {
					imageResId = R.drawable.greenbutton;
					state = false;
					invalidate();
				} else {
					imageResId = R.drawable.redbutton;
					state = true;
					invalidate();
				}
				if(view!=null){
					view.setOrigin(state);
				}
			}
			return true;
		}
	}
	
	public static void registerView(RightSideView e){
		view=e;
	}
}
